<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Returns new DateTime object formatted according to the specified format</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="datetime.construct.html">DateTime::__construct</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="datetime.getlasterrors.html">DateTime::getLastErrors</a></div>
 <div class="up"><a href="class.datetime.html">DateTime</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="datetime.createfromformat" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">DateTime::createFromFormat</h1>
  <h1 class="refname">date_create_from_format</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0)</p><p class="refpurpose"><span class="refname">DateTime::createFromFormat</span> -- <span class="refname">date_create_from_format</span> &mdash; <span class="dc-title">Returns new DateTime object formatted according to the specified format</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-datetime.createfromformat-description">
  <h3 class="title">说明</h3>
  <p class="para">面向对象风格</p>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type"><a href="class.datetime.html" class="type DateTime">DateTime</a></span> <span class="methodname"><strong>DateTime::createFromFormat</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$time</code></span>
   [, <span class="methodparam"><span class="type"><a href="class.datetimezone.html" class="type DateTimeZone">DateTimeZone</a></span> <code class="parameter">$timezone</code></span>
  ] )</div>

  <p class="para rdfs-comment">过程化风格</p>
  <div class="methodsynopsis dc-description">
   <span class="type"><a href="class.datetime.html" class="type DateTime">DateTime</a></span> <span class="methodname"><a href="function.date-create-from-format.html" class="methodname">date_create_from_format</a></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$time</code></span>
   [, <span class="methodparam"><span class="type"><a href="class.datetimezone.html" class="type DateTimeZone">DateTimeZone</a></span> <code class="parameter">$timezone</code></span>
  ] )</div>

  <p class="para rdfs-comment">
   Returns new DateTime object formatted according to the specified format.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-datetime.createfromformat-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>
<em><code class="parameter">format</code></em></dt>

    <dd>

     <p class="para">
      The format that the passed in <span class="type"><a href="language.types.string.html" class="type string">string</a></span> should be in. See the
      formatting options below. In most cases, the same letters as for the
      <span class="function"><a href="function.date.html" class="function">date()</a></span> can be used.
     </p>
     <p class="para">
      <table class="doctable table">
       <caption><strong>The following characters are recognized in the
       <em><code class="parameter">format</code></em> parameter string</strong></caption>
       
        <thead>
         <tr>
          <th><em><code class="parameter">format</code></em> character</th>
          <th>Description</th>
          <th>Example parsable values</th>
         </tr>

        </thead>

        <tbody class="tbody">
         <tr>
          <td style="text-align: center;"><em class="emphasis">Day</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td><em>d</em> and <em>j</em></td>
          <td>Day of the month, 2 digits with or without leading zeros</td>
          <td>
           <em>01</em> to <em>31</em> or
           <em>1</em> to <em>31</em>
          </td>
         </tr>

         <tr>
          <td><em>D</em> and <em>l</em></td>
          <td>A textual representation of a day</td>
          <td>
           <em>Mon</em> through <em>Sun</em> or
           <em>Sunday</em> through <em>Saturday</em>
          </td>
         </tr>

         <tr>
          <td><em>S</em></td>
          <td>English ordinal suffix for the day of the month, 2
          characters. It&#039;s ignored while processing.</td>
          <td>
           <em>st</em>, <em>nd</em>, <em>rd</em> or
           <em>th</em>.
          </td>
         </tr>

         <tr>
          <td><em>z</em></td>
          <td>The day of the year (starting from 0)</td>
          <td><em>0</em> through <em>365</em></td>
         </tr>

         <tr>
          <td style="text-align: center;"><em class="emphasis">Month</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td><em>F</em> and <em>M</em></td>
          <td>A textual representation of a month, such as January or Sept</td>
          <td>
           <em>January</em> through <em>December</em> or
           <em>Jan</em> through <em>Dec</em>
          </td>
         </tr>

         <tr>
          <td><em>m</em> and <em>n</em></td>
          <td>Numeric representation of a month, with or without leading zeros</td>
          <td>
           <em>01</em> through <em>12</em> or
           <em>1</em> through <em>12</em>
          </td>
         </tr>

         <tr>
          <td style="text-align: center;"><em class="emphasis">Year</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td><em>Y</em></td>
          <td>A full numeric representation of a year, 4 digits</td>
          <td>Examples: <em>1999</em> or <em>2003</em></td>
         </tr>

         <tr>
          <td><em>y</em></td>
          <td>A two digit representation of a year</td>
          <td>Examples: <em>99</em> or <em>03</em></td>
         </tr>

         <tr>
          <td style="text-align: center;"><em class="emphasis">Time</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td><em>a</em> and <em>A</em></td>
          <td>Ante meridiem and Post meridiem</td>
          <td><em>am</em> or <em>pm</em></td>
         </tr>

         <tr>
          <td><em>g</em> and <em>h</em></td>
          <td>12-hour format of an hour with or without leading zero</td>
          <td>
           <em>1</em> through <em>12</em> or
           <em>01</em> through <em>12</em>
          </td>
         </tr>

         <tr>
          <td><em>G</em> and <em>H</em></td>
          <td>24-hour format of an hour with or without leading zeros</td>
          <td>
           <em>0</em> through <em>23</em> or
           <em>00</em> through <em>23</em>
          </td>
         </tr>

         <tr>
          <td><em>i</em></td>
          <td>Minutes with leading zeros</td>
          <td><em>00</em> to <em>59</em></td>
         </tr>

         <tr>
          <td><em>s</em></td>
          <td>Seconds, with leading zeros</td>
          <td><em>00</em> through <em>59</em></td>
         </tr>

         <tr>
          <td><em>u</em></td>
          <td>Microseconds (up to six digits)</td>
          <td>Example: <em>45</em>, <em>654321</em></td>
         </tr>

         <tr>
          <td style="text-align: center;"><em class="emphasis">Timezone</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td>
           <em>e</em>, <em>O</em>,
           <em>P</em> and <em>T</em>
          </td>
          <td>Timezone identifier, or difference to UTC in hours, or
          difference to UTC with colon between hours and minutes, or timezone
          abbreviation</td>
          <td>Examples: <em>UTC</em>, <em>GMT</em>,
           <em>Atlantic/Azores</em> or 
           <em>+0200</em> or <em>+02:00</em> or
           <em>EST</em>, <em>MDT</em>
          </td>
         </tr>

         <tr>
          <td style="text-align: center;"><em class="emphasis">Full Date/Time</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td><em>U</em></td>
          <td>Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT)</td>
          <td>Example: <em>1292177455</em></td>
         </tr>

         <tr>
          <td style="text-align: center;"><em class="emphasis">Whitespace and Separators</em></td>
          <td>---</td>
          <td>---</td>
         </tr>

         <tr>
          <td><em> </em> (space)</td>
          <td>One space or one tab</td>
          <td>Example: <em> </em></td>
         </tr>

         <tr>
          <td><em>#</em></td>
          <td>
           One of the following separation symbol: <em>;</em>,
           <em>:</em>, <em>/</em>, <em>.</em>,
           <em>,</em>, <em>-</em>, <em>(</em> or
           <em>)</em>
          </td>
          <td>Example: <em>/</em></td>
         </tr>

         <tr>
          <td>
           <em>;</em>,
           <em>:</em>, <em>/</em>, <em>.</em>,
           <em>,</em>, <em>-</em>, <em>(</em> or
           <em>)</em>
          </td>
          <td>The specified character.</td>
          <td>Example: <em>-</em></td>
         </tr>

         <tr>
          <td><em>?</em></td>
          <td>A random byte</td>
          <td>Example: <em>^</em> (Be aware that for UTF-8
          characracters you might need more than one <em>?</em>.
          In this case, using <em>*</em> is probably what you want
          instead)</td>
         </tr>

         <tr>
          <td><em>*</em></td>
          <td>Random bytes until the next separator or digit</td>
          <td>Example: <em>*</em> in <em>Y-*-d</em> with
          the string <em>2009-aWord-08</em> will match
          <em>aWord</em></td>
         </tr>

         <tr>
          <td><em>!</em></td>
          <td>Resets all fields (year, month, day, hour, minute, second,
          fraction and timzone information) to the Unix Epoch</td>
          <td>Without <em>!,</em> all fields will be set to the
          current date and time.</td>
         </tr>

         <tr>
          <td><em>|</em></td>
          <td>Resets all fields (year, month, day, hour, minute, second,
          fraction and timzone information) to the Unix Epoch if they have
          not been parsed yet</td>
          <td><em>Y-m-d|</em> will set the year, month and day
          to the information found in the string to parse, and sets the hour,
          minute and second to 0.</td>
         </tr>

         <tr>
          <td><em>+</em></td>
          <td>If this format specifier is present, trailing data in the
          string will not cause an error, but a warning instead</td>
          <td>Use <span class="methodname"><a href="datetime.getlasterrors.html" class="methodname">DateTime::getLastErrors()</a></span> to find out
          whether trailing data was present.</td>
         </tr>

        </tbody>
       
      </table>

     </p>
     <p class="para">
      Unrecognized characters in the format string will cause the
      parsing to fail and an error message is appended to the returned
      structure. You can query error messages with
      <span class="methodname"><a href="datetime.getlasterrors.html" class="methodname">DateTime::getLastErrors()</a></span>.
     </p>       
     <p class="para">
      If <em><code class="parameter">format</code></em> does not contain the character
      <em>!</em> then portions of the generated time which are not
      specified in <em><code class="parameter">format</code></em> will be set to the current
      system time.
     </p>
     <p class="para">
      If <em><code class="parameter">format</code></em> contains the
      character <em>!</em>, then portions of the generated
      time not provided in <em><code class="parameter">format</code></em>, as well as
      values to the left-hand side of the <em>!</em>, will
      be set to corresponding values from the Unix epoch.
     </p>
     <p class="para">
      The Unix epoch is 1970-01-01 00:00:00 UTC.
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">time</code></em></dt>

    <dd>

     <p class="para">
      String representing the time.
     </p>
    </dd>

   
   
    <dt>
<em><code class="parameter">timezone</code></em></dt>

    <dd>

     <p class="para">
      A <a href="class.datetimezone.html" class="classname">DateTimeZone</a> object representing the
      desired time zone.
     </p>
     <p class="para">
      If <em><code class="parameter">timezone</code></em> is omitted and
      <em><code class="parameter">time</code></em> contains no timezone,
      the current timezone will be used.
     </p>
     <blockquote class="note"><p><strong class="note">Note</strong>: 
      <p class="para">
       The <em><code class="parameter">timezone</code></em> parameter
       and the current timezone are ignored when the
       <em><code class="parameter">time</code></em> parameter either
       contains a UNIX timestamp (e.g. <em>946684800</em>)
       or specifies a timezone
       (e.g. <em>2010-01-28T15:00:00+02:00</em>).
      </p>
     </p></blockquote>
    </dd>

   
  </dl>

 </div>


 <div class="refsect1 returnvalues" id="refsect1-datetime.createfromformat-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns a new DateTime instance 或者在失败时返回 <strong><code>FALSE</code></strong>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-datetime.createfromformat-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-2189">
   <p><strong>Example #1 <span class="function"><strong>DateTime::createFromFormat()</strong></span> example</strong></p>
   <div class="example-contents"><p>面向对象风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #DD0000">'j-M-Y'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'15-Feb-2009'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>过程化风格</p></div>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">date_create_from_format</span><span style="color: #007700">(</span><span style="color: #DD0000">'j-M-Y'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'15-Feb-2009'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #0000BB">date_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$date</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程会输出：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
2009-02-15
</pre></div>
   </div>
  </div>
  <div class="example" id="example-2190">
   <p><strong>Example #2 Intricacies of <span class="function"><strong>DateTime::createFromFormat()</strong></span></strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'Current&nbsp;time:&nbsp;'&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d&nbsp;H:i:s'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'2009-02-15'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Format:&nbsp;</span><span style="color: #0000BB">$format</span><span style="color: #DD0000">;&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d&nbsp;H:i:s'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Y-m-d&nbsp;H:i:s'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'2009-02-15&nbsp;15:16:17'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Format:&nbsp;</span><span style="color: #0000BB">$format</span><span style="color: #DD0000">;&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d&nbsp;H:i:s'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Y-m-!d&nbsp;H:i:s'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'2009-02-15&nbsp;15:16:17'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Format:&nbsp;</span><span style="color: #0000BB">$format</span><span style="color: #DD0000">;&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d&nbsp;H:i:s'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$format&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'!d'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$date&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">DateTime</span><span style="color: #007700">::</span><span style="color: #0000BB">createFromFormat</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'15'</span><span style="color: #007700">);<br />echo&nbsp;</span><span style="color: #DD0000">"Format:&nbsp;</span><span style="color: #0000BB">$format</span><span style="color: #DD0000">;&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$date</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d&nbsp;H:i:s'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
   </div>

   <div class="example-contents"><p>以上例程的输出类似于：</p></div>
   <div class="example-contents screen">
<div class="cdata"><pre>
Current time: 2010-04-23 10:29:35
Format: Y-m-d; 2009-02-15 10:29:35
Format: Y-m-d H:i:s; 2009-02-15 15:16:17
Format: Y-m-!d H:i:s; 1970-01-15 15:16:17
Format: !d; 1970-01-15 00:00:00
</pre></div>
   </div>
  </div>
 </div>


 <div class="refsect1 seealso" id="refsect1-datetime.createfromformat-seealso">
  <h3 class="title">参见</h3>
  <ul class="simplelist">
   <li class="member"><span class="function"><a href="datetime.construct.html" class="function" rel="rdfs-seeAlso">DateTime::__construct()</a> - Returns new DateTime object</span></li>
   <li class="member"><span class="function"><a href="datetime.getlasterrors.html" class="function" rel="rdfs-seeAlso">DateTime::getLastErrors()</a> - Returns the warnings and errors</span></li>
   <li class="member"><span class="function"><a href="function.checkdate.html" class="function" rel="rdfs-seeAlso">checkdate()</a> - 验证一个格里高里日期</span></li>
   <li class="member"><span class="function"><a href="function.strptime.html" class="function" rel="rdfs-seeAlso">strptime()</a> - 解析由 strftime 生成的日期／时间</span></li>
  </ul>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="datetime.construct.html">DateTime::__construct</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="datetime.getlasterrors.html">DateTime::getLastErrors</a></div>
 <div class="up"><a href="class.datetime.html">DateTime</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
